<template>
  <div style="height:100%">
    <el-form ref="form" :model="form" :rules="rules" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-row :gutter="2">
        <el-col :span="12">
          <el-form-item label="组织机构" prop="busMainData.sysOrgCode">
            <OrgsetTreeSelect v-model="form.busMainData.sysOrgCode" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" :has-priv="false" style="width: 200px" />
          </el-form-item>
          <el-form-item label="业务时间" prop="busMainData.busDate">
            <el-date-picker
              v-model="form.busMainData.busDate"
              placeholder="业务时间"
              type="datetime"
              size="mini"
              clearable
              style="width: 200px"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="备注" prop="busMainData.busRmk">
            <el-input
              v-model="form.busMainData.busRmk"
              size="mini"
              class="filter-item" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应商" prop="busMainData.otherSysOwnerCpy">
            <CpyOrgSetSelectDialog v-model="form.busMainData.busOtherCpyName" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" :cpy-code="form.busMainData.otherSysOwnerCpy" :ori-org-code="form.busMainData.otherSysOrgCode" cpy-owner-type="supply" @cpySelectCallback="cpySelectCallback" @orgSetSelectCallback="orgSetSelectCallback" />
          </el-form-item>
          <el-form-item label="有效时间" prop="busMainData.validDate">
            <el-date-picker
              v-model="form.busMainData.validDate"
              placeholder="有效时间"
              type="date"
              size="mini"
              clearable
              style="width: 200px"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="附档" prop="attachmentList">
            <FileUpload
              ref="fileUpload"
              :editable="form.busPageInfo.editable"
              :file-limit="5"
              :is-direct-upload="true"
              list-type="list" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <inquiry-detail ref="inquiryDetail" :inquiry-details="form.inquiryDetails" :editable="form.busPageInfo.editable" />
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','PURCHASE_INQUIRY_ALL','PURCHASE_INQUIRY_CHANGE','PURCHASE_INQUIRY_CREATE']" :loading="loading" type="primary" @click="doSave">暂存</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','PURCHASE_INQUIRY_ALL','PURCHASE_INQUIRY_SUBMIT']" :loading="loading" type="primary" @click="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
import inquiry from '@/api/business/Inquiry'
import CpyOrgSetSelectDialog from '@/views/components/CpyOrgSetSelectDialog'
import OrgsetTreeSelect from '@/views/components/OrgsetTreeSelect'
import inquiryDetail from './inquiryDetail'

export default {
  components: { CpyOrgSetSelectDialog, OrgsetTreeSelect, inquiryDetail },
  dictTypes: ['item_unit'],
  props: {
  },
  data() {
    return {
      loading: false,
      style: 'width: 184px',
      form: {
        busMainData: {
          id: '',
          enableFlag: 'Y'
        },
        inquiryDetails: [],
        busPageInfo: {
          editable: false
        }
      },
      rules: {
      }
    }
  },
  mounted() {
    this.$emit('afterMounted')
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    getOtherDetailData(mainId, replaceFlag, replaceDetailLotFlag) {
      if (mainId) {
        inquiry.getDetailData(mainId, replaceFlag, replaceDetailLotFlag).then(res => {
          this.form.inquiryDetails = res.data
        })
      }
      setTimeout(() => {
        this.$refs.fileUpload.setFileList(this.form.attachmentList)
      }, 200)
    },
    viewApproveData(busId) {
      const data = { paramBean: { busId: busId }}
      inquiry.toViewApprove(data).then(res => {
        this.form = res.data
        this.getOtherDetailData(this.form.busMainData.id, false, false)
      })
    },
    cpySelectCallback(selectedRow, selectObj) {
      this.form.busMainData.otherSysOwnerCpy = ''
      this.form.busMainData.busOtherCpyName = ''
      if (selectedRow) {
        this.form.busMainData.otherSysOwnerCpy = selectedRow.cpyCode
        this.form.busMainData.busOtherCpyName = selectedRow.cpyLongName
      }
      selectObj.cancel()
    },
    orgSetSelectCallback(orgCode) {
      this.form.busMainData.otherSysOrgCode = orgCode
    },
    setDetails(details) {
      this.form.inquiryDetails = details
    }
  }
}
</script>
